/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import utils.DateUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class ReportForm  extends Form implements ActionListener
{
    public static final int TIME_TYPE = 1;
    public static final int COUNT_TYPE = 2;

    public static final int SHOW_ID = 1;
    public static final int BACK_ID = 2;

    private Command show;
    private Command back;
    private FormAction listener;

    private ComboBox reportType;
    private TextField reportFrom;
    private TextField reportTo;

    public ReportForm(FormAction listener)
    {
        super("Отчеты");
        this.listener = listener;

        init();
    }

    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addCommandListener(this);

        back= new Command("Назад", BACK_ID);
        addCommand(back);

        show = new Command("Показать", SHOW_ID);
        addCommand(show);

        String typesList[] = {"По времени"};//,"По заказам"};
        addComponent(new Label("Тип отчета: "));
	reportType = new ComboBox(typesList);
        reportType.setSelectCommandText("");
        addComponent(reportType);

        addComponent(new Label("От:"));
        reportFrom = new TextField();
        reportFrom.setSelectCommandText("");
        reportFrom.setInputMode("123");
        reportFrom.setConstraint(TextArea.ANY);
        addComponent(reportFrom);

        addComponent(new Label("До:"));
        reportTo = new TextField();
        reportTo.setSelectCommandText("");
        reportTo.setInputMode("123");
        reportTo.setConstraint(TextArea.ANY);
        addComponent(reportTo);

        addComponent(new Label("  "));
    }

    public void clear()
    {
        removeAll();

        addComponent(new Label("Тип отчета: "));
        addComponent(reportType);

        addComponent(new Label("От:"));
        addComponent(reportFrom);

        addComponent(new Label("До:"));
        addComponent(reportTo);
    }

    public void add(String value)
    {
        TextArea text = new TextArea(value, 1, 30);
        text.setAlignment(TextArea.LEFT);
        text.setEditable(false);
        text.setFocusable(true);
        text.getStyle().setBorder(Border.createEmpty());
        addComponent(text);

        getContentPane().revalidate();
        revalidate();
    }

    public void show(boolean reset)
    {
        //Очищаем форму
        if (reset)
        {
            reportType.setSelectedIndex(0);

            String currentdate = DateUtils.getCurrentDate();

            reportFrom.setText(DateUtils.calculateDate(currentdate, -7));

            reportTo.setText(currentdate);
        }

        //Отображаем форму
        super.show();
    }

    public int getType()
    {
        switch(reportType.getSelectedIndex())
        {
            case 0 : return TIME_TYPE;
            case 1: return COUNT_TYPE;
            default: return TIME_TYPE;
        }
    }

    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            listener.commandAction(evt.getCommand().getId());
        }
    }

    public long getDateForm()
    {
        if (!Utils.isInvalidDate(reportFrom.getText()))
        {
            return DateUtils.getTicks(reportFrom.getText());
        }
        else
        {
            return -1;
        }
    }

    public long getDateTo()
    {
        if (!Utils.isInvalidDate(reportTo.getText()))
        {
            return DateUtils.getTicks(reportTo.getText());
        }
        else
        {
            return -1;
        }
    }
}
